﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sora.Data.DataFormatProviders;
using System.IO;
using System.Windows.Documents;
using System.Windows;
using Sora.Data;

namespace Sora.HelpSystem.Package.DataFormatters
{
    public class FlowDocumentDataFormatProvider : IDataFormatProvider
    {
        #region IDataFormatProvider Members

        public object FromStream(IDatabase caller, Stream source)
        {
            if (source == null || source.Length <= 0)
                return new FlowDocument(); //vide
            else
            {
                FlowDocument res = new FlowDocument();

                TextRange rg = new TextRange(res.ContentStart, res.ContentEnd);
                rg.Load(source, DataFormats.XamlPackage);

                return res;
            }
        }

        public Type GetValueType()
        {
            return typeof(FlowDocument);
        }

        public void ToStream(IDatabase caller, Stream target, object value)
        {
            if (value != null)
            {
                FlowDocument doc = (FlowDocument)value;

                TextRange rg = new TextRange(doc.ContentStart, doc.ContentEnd);
                rg.Save(target, DataFormats.XamlPackage);
            }
        }

        #endregion
    }
}
